package com.zking.frame;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.*;
import javax.swing.border.MatteBorder;
import javax.xml.transform.Source;

import com.zking.dao.UserDao;
import com.zking.entity.User;
import com.zking.utils.Tool工具;
import javafx.scene.control.PasswordField;

//开始页面
public class Land extends JFrame {

	private JLabel textJLabel, userJLabel, passwordJLabel, retrieveJLabel;
	private JTextField userField;
	private JPasswordField passwordField;
	private JButton landButton, registerButton;
	private JPanel jPanel;
	private List<User> usList;
	private volatile boolean ab = false;
	private volatile boolean bc = false;

	public Land() {

	}

	public Land(JFrame a) {
		a.dispose();
		// 获取jframe页面
		JFrame jFrame = Tool工具.getJFrame();
		jFrame.getContentPane().setBackground(Color.white);


		jPanel = new JPanel();
		jPanel.setBounds(20, 50, 600, 900);
		jPanel.setLayout(null);
		jPanel.setBorder(new MatteBorder(2, 2, 2, 2, new Color(63, 247, 18)));
		jFrame.add(jPanel, 0);
		// 设置图片
		ImageIcon icon = new ImageIcon(Land.class.getResource("登陆.jpg"));
		JLabel jLabel = new JLabel(icon);
		jLabel.setBounds(0, 0, 600, 200);
		jPanel.add(jLabel, -1);
		ImageIcon icon2 = new ImageIcon(Land.class.getResource("登陆背景.jpg"));
		JLabel jLabel2 = new JLabel(icon2);
		jLabel2.setBounds(0, 200, 600, 700);
		jPanel.add(jLabel2, -1);
		// 设置提示框
		textJLabel = new JLabel("欢迎来到——吃了没");
		textJLabel.setForeground(new Color(18, 220, 214));
		textJLabel.setFont(new Font("华文琥珀", Font.PLAIN, 40));
		textJLabel.setBounds(110, 180, 400, 100);
		jPanel.add(textJLabel, 0);
		// 设置用户名和密码提示
		userJLabel = new JLabel("用户名：");
		userJLabel.setBounds(130, 350, 150, 50);
		userJLabel.setFont(new Font("黑体", Font.BOLD, 25));
		jPanel.add(userJLabel, 0);
		passwordJLabel = new JLabel("密码：");
		passwordJLabel.setBounds(130, 450, 100, 50);
		passwordJLabel.setFont(new Font("黑体", Font.BOLD, 25));
		jPanel.add(passwordJLabel, 0);
		// 设置用户名和密码文本框
		userField = new JTextField("请输入用户名");
		userField.setBounds(230, 350, 200, 50);
		userField.setFont(new Font("黑体", Font.BOLD, 20));
		userField.setForeground(Color.GRAY);
		jPanel.add(userField, 0);

		// 设置焦点事件
		userField.addFocusListener(new FocusListener() {

			@Override
			public void focusLost(FocusEvent e) {
				// TODO Auto-generated method stub
				ab = false;
				for (int i = 0; i < usList.size(); i++) {
					if (usList.get(i).getUserName().equals(userField.getText())) {
						Tool工具 t = new Tool工具();
						t.setName(userField.getText());
						ab = true;
						return;
					}
				}
				if (userField.getText().equals("")) {
					userField.setText("请输入用户名");
					userField.setForeground(Color.GRAY);
				}
				if (!ab) {
					userField.setText("用户名错误");
					userField.setForeground(Color.GRAY);
				}

			}

			@Override
			public void focusGained(FocusEvent e) {
				// TODO Auto-generated method stub
				if (userField.getText().equals("用户名错误") || userField.getText().equals("请输入用户名")) {
					userField.setText("");
					userField.setForeground(Color.black);
				}
			}
		});

		passwordField = new JPasswordField();
		passwordField.setBounds(230, 450, 200, 50);
		passwordField.setFont(new Font("黑体", Font.BOLD, 15));
		passwordField.setForeground(Color.GRAY);
		jPanel.add(passwordField, 0);
		JLabel jLabel1 = new JLabel("",0);
		jLabel1.setBounds(230,500,200,50);
		jLabel1.setForeground(Color.GRAY);
		jLabel1.setFont(new Font("黑体",Font.BOLD,15));
		jPanel.add(jLabel1, 0);

		// 设置焦点事件
		passwordField.addFocusListener(new FocusListener() {

			@Override
			public void focusLost(FocusEvent e) {
				bc = false;
				// TODO Auto-generated method stub
				for (int i = 0; i < usList.size(); i++) {
					if (usList.get(i).getUserName().equals(userField.getText())){
						if (usList.get(i).getPassWord().equals(passwordField.getText())) {
							bc = true;
							break;
						}
					}

				}
				if (passwordField.getText().equals("")){
					jLabel1.setText("请输入密码");
					jLabel1.setForeground(Color.red);
				}else{
					if(!bc){
						jLabel1.setText("密码错误");
						jLabel1.setForeground(Color.red);
					}else{
						jLabel1.setText("密码正确");
						jLabel1.setForeground(Color.green);
						return;
					}
				}
			}

			@Override
			public void focusGained(FocusEvent e) {
				// TODO Auto-generated method stub
				jLabel1.setText("密码6-12位字母汉字");
				jLabel1.setForeground(Color.black);
			}
		});

		// 设置按钮
		registerButton = new JButton("注册");
		registerButton.setBounds(130, 550, 100, 50);
		registerButton.setFont(new Font("黑体", Font.BOLD, 15));
		registerButton.setBackground(new Color(203, 232, 234));
		registerButton.setCursor(new Cursor(Cursor.HAND_CURSOR));

		// 设置事件
		registerButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				new Register(jFrame);
			}
		});
		jPanel.add(registerButton, 0);

		landButton = new JButton("登陆");
		landButton.setBounds(240, 550, 190, 50);
		landButton.setFont(new Font("黑体", Font.BOLD, 15));
		landButton.setBackground(new Color(203, 232, 234));
		landButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
		// 设置点击事件
		landButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stubm
				if (ab && bc) {
					for (int a=0;a<usList.size();a++){
						if (usList.get(a).getUserName().equals(userField.getText())){
							if (!usList.get(a).getSort().equals("1")){
								new Client(jFrame,usList.get(a).getShopname(),usList.get(a).getSort());
							}else {
								new HomePage(jFrame);
							}
						}
					}

				}
			}
		});
		jPanel.add(landButton, 0);

		JLabel administratorsJLabel = new JLabel("注册管理员");
		administratorsJLabel.setBounds(430, 575, 100, 30);
		administratorsJLabel.setFont(new Font("黑体", Font.BOLD, 15));
		administratorsJLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
		administratorsJLabel.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 1) {
					new Administrators(jFrame);
				}
			}

			// 进入
			public void mouseEntered(MouseEvent e) {
				administratorsJLabel.setForeground(Color.white);
			}

			// 离开
			public void mouseExited(MouseEvent e) {
				administratorsJLabel.setForeground(Color.black);
			}
		});
		jPanel.add(administratorsJLabel, 0);
		JLabel retrievePassword = new JLabel("找回密码");
		retrievePassword.setBounds(520, 575, 100, 30);
		retrievePassword.setFont(new Font("黑体", Font.BOLD, 15));
		retrievePassword.setCursor(new Cursor(Cursor.HAND_CURSOR));
		retrievePassword.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 1) {
					new Retrieve(jFrame);
				}
			}

			// 进入
			public void mouseEntered(MouseEvent e) {
				retrievePassword.setForeground(Color.white);
			}

			// 离开
			public void mouseExited(MouseEvent e) {
				retrievePassword.setForeground(Color.black);
			}
		});
		jPanel.add(retrievePassword, 0);
		// 刷新页面
		jFrame.repaint();
		jFrame.revalidate();
		// 开始时自动获取用户列表
		UserDao userDao = new UserDao();
		usList = userDao.queryStudents();


	}

	public static void main(String[] args) {
		Land land = new Land();
		new Land(land);

	}

}
